Date.prototype.format = function (format) {
    var o = {
        'M+': this.getMonth() + 1, // month
        'd+': this.getDate(), // day
        'h+': this.getHours(), // hour
        'm+': this.getMinutes(), // minute
        's+': this.getSeconds(), // second
        'q+': Math.floor((this.getMonth() + 3) / 3), // quarter
        'S': this.getMilliseconds() // millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
    for (var k in o) {
        if (new RegExp('(' + k + ')').test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
        }
    }
    return format
}
const tool = {
    // 年的截取
    year(dates) {
        if (dates) {
            var date = new Date(dates)
            return date.format('yyyy-MM-dd')
        } else {
            return "-"
        }
    },
    yDate(dates) {
        if (dates) {
            var date = new Date(dates)
            return date.format('yyyy-MM-dd hh:mm')
        } else {
            return "-"
        }
    },
    sDate(dates) {
        if (dates) {
            var date = new Date(dates)
            return date.format('yyyyMMddhhmmss')
        } else {
            return "-"
        }
    },
    dateForm(dates, format){
        if (dates) {
            var date = new Date(dates)
            return date.format(format)
        } else {
            return "-"
        }
    },
    date(dates) {
        if (dates) {
            var date = new Date(dates)
            return date.format('yyyy-MM-dd hh:mm:ss')
        } else {
            return "-"
        }
    },
    //计算两个时间格式的字符串相差的周数、月数、年数
    //‘2019-3-12， 2019-4-29’ ==》 {year:0,month:1,week:xx}
    diff(sDate1, sDate2) {
        var fixDate = function (sDate) {
            var aD = sDate.split('-');
            for (var i = 0; i < aD.length; i++) {
                aD[i] = fixZero(parseInt(aD[i]));
            }
            return aD.join('-');
        };
        var fixZero = function (n) {
            return n < 10 ? '0' + n : n;
        };
        var fixInt = function (a) {
            for (var i = 0; i < a.length; i++) {
                a[i] = parseInt(a[i]);
            }
            return a;
        };
        var getMonthDays = function (y, m) {
            var aMonthDays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
            if ((y % 400 == 0) || (y % 4 == 0 && y % 100 != 0)) {
                aMonthDays[2] = 29;
            }
            return aMonthDays[m];
        };
        var y = 0;
        var m = 0;
        var d = 0;
        var sTmp;
        var aTmp;
        sDate1 = fixDate(sDate1);
        sDate2 = fixDate(sDate2);
        if (sDate1 > sDate2) {
            sTmp = sDate2;
            sDate2 = sDate1;
            sDate1 = sTmp;
        }
        var aDate1 = sDate1.split('-');
        aDate1 = fixInt(aDate1);
        var aDate2 = sDate2.split('-');
        aDate2 = fixInt(aDate2);
        //计算相差的年份
        /*aTmp = [aDate1[0]+1,fixZero(aDate1[1]),fixZero(aDate1[2])];
        while(aTmp.join('-') <= sDate2){
            y++;
            aTmp[0]++;
        }*/
        y = aDate2[0] - aDate1[0];
        if (sDate2.replace(aDate2[0], '') < sDate1.replace(aDate1[0], '')) {
            y = y - 1;
        }
        //计算月份
        aTmp = [aDate1[0] + y, aDate1[1], fixZero(aDate1[2])];
        while (true) {
            if (aTmp[1] == 12) {
                aTmp[0]++;
                aTmp[1] = 1;
            } else {
                aTmp[1]++;
            }
            if (([aTmp[0], fixZero(aTmp[1]), aTmp[2]]).join('-') <= sDate2) {
                m++;
            } else {
                break;
            }
        }
        //计算天数
        aTmp = [aDate1[0] + y, aDate1[1] + m, aDate1[2]];
        if (aTmp[1] > 12) {
            aTmp[0]++;
            aTmp[1] -= 12;
        }
        while (true) {
            if (aTmp[2] == getMonthDays(aTmp[0], aTmp[1])) {
                aTmp[1]++;
                aTmp[2] = 1;
            } else {
                aTmp[2]++;
            }
            sTmp = ([aTmp[0], fixZero(aTmp[1]), fixZero(aTmp[2])]).join('-');
            if (sTmp <= sDate2) {
                d++;
            } else {
                break;
            }
        }
        //讲天数换成周数
        d = d % 7 === 0 ? d / 7 : parseInt(d / 7) + 1;
        return {year: y, month: m, week: d};
    }
}

export default tool
